﻿//--------------------------------------------------------------------------------------------------------------------- 
// <copyright file="CheckOutFiles.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights reserved.
// </copyright>
// <summary>
//   Defines the CheckOutFile type.
// </summary>
//---------------------------------------------------------------------------------------------------------------------
using Collaboris.Saf.Actions.Wss.Entities;

namespace Collaboris.Saf.Actions.Wss.File
{
    using System.Collections.Generic;

    using Engine;

    using Microsoft.SharePoint;

    using SharePoint.Wss.Attributes;
    using SharePoint.Wss.Files;

    using Utils;

     [CollaborisClass(
        Title = "Checkout File",
        ReleaseStatus = ReleaseStatus.Stable,
        DisableDocumentation = false,
        Remarks = "<p>This action can also receive a list of files (to checkout) from other actions. " +
                    @"Please refer to the <a href=""Collaboris.Saf.Actions.Wss.File.GetFiles.html"">GetFiles Action</a>" +
                    " for an example of how this works.</p>" +
                    "The Undo will also discard the checkout of any files supplied in the in the list of files.",
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Used to check out a list of files in a one or more document libraries.",
        ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C646}",
        UndoImplemented = true
    )
    ]
    public class CheckOutFile : WSSActionBase<CheckOutFileInfo>
    {
        #region Methods (6) 

        /// <summary>
        /// This method will CheckOut a File.
        /// </summary>
        /// <param name="context">
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, CheckOutFileInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            ValidationHelper.VerifyObjectArgument(TargetWeb, "TargetWeb");
            ValidationHelper.VerifyObjectArgument(entity, "entity");
 
            ProcessStatus status = this.RunCheckOutFile(entity, context.Web, ProcessMode.Do);

            this.Trace.TraceMethodEnd("DoAction");

            return status;
        }

        /// <summary>
        /// This method will "Undo" the Current Checkout.
        /// </summary>
        /// <param name="context">
        /// IContext defining the current context.
        /// </param>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// ProcessStatus signalling to the Macro what its outcome was.
        /// </returns>
        protected override ProcessStatus UndoAction(SafContext context, CheckOutFileInfo entity)
        {
            this.Trace.TraceMethodStart("UndoAction");
            
            ValidationHelper.VerifyObjectArgument(TargetWeb, "TargetWeb");
            ValidationHelper.VerifyObjectArgument(entity, "entity");

            ProcessStatus status = this.RunCheckOutFile(entity, context.Web, ProcessMode.Undo);

            this.Trace.TraceMethodEnd("UndoAction");

            return status;
        }

        /// <summary>
        /// Checks the Data entity for Validity
        /// </summary>
        /// <param name="context">The <see cref="IContext"/>context.</param>
        /// <param name="entity">The data entity to Validate</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/> signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid</returns>
        protected override bool IsValid(SafContext context, CheckOutFileInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateObjectForNull(entity.FileUrls, "entity.FileUrls", ref errors);                       
            
            // Have we added errors ?
            return errors.Count == currentCount;
        }

        /// <summary>
        /// Checks out the specified the file as spec'd in <see cref="ListItemInfo"/>
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="web">The web.</param>
        /// <param name="mode">The mode.</param>
        /// <returns></returns>
        private ProcessStatus RunCheckOutFile(CheckOutFileInfo entity, SPWeb web, ProcessMode mode)
        {
            this.Trace.TraceMethodStart("RunCheckOutFile");            

            if (mode == ProcessMode.Do)
            {   
                SPFileHelper.CheckOutFiles(TargetWeb,entity.FileUrls);
            }
            if (mode == ProcessMode.Undo)
            {                
                SPFileHelper.DiscardCheckOutOfFiles(TargetWeb,entity.FileUrls);
            }            

            this.Trace.TraceMethodEnd("RunCheckOutFile");            

            return ProcessStatus.Success;

        }

        #endregion Methods 
    }
}